/**
* Author: Samfu
* Time  : 下午11:35:13
* Desc  : 
*/
package com.samfu.spider.core;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.samfu.spider.bean.LinkTypeData;
import com.samfu.spider.exception.RuleException;
import com.samfu.spider.util.Rule;

public class ExtractService {

	public static List<LinkTypeData> extract(Rule rule) throws IOException {
		
		//进行rule的检验
		validateRule(rule);
		
		List<LinkTypeData> datas = new ArrayList<LinkTypeData>();
		LinkTypeData data = null;
		
		String url = rule.getUrl();
		String[] params = rule.getParams();
		String[] values = rule.getValues();
		String resultTagName = rule.getResultTagName();
		int type = rule.getType();
		int requestType = rule.getRequestMethod();
		
		Connection connection = Jsoup.connect(url);
		
		//设置请求参数
		if (params != null) {
			for (int i=0; i<params.length; i++) {
				connection.data(params[i], values[i]);
			}
		}
		
		//设置请求类型
		Document document = null;
		switch(requestType) {
		case Rule.GET: 
			document = connection.timeout(100000).get();
			break;
		case Rule.POST:
			document = connection.timeout(100000).post();
			break;
			default: break;
		}
		
		//处理返回数据
		Elements results = new Elements();
		switch (type) {
		case Rule.CLASS:
			results = document.getElementsByClass(resultTagName);
			break;
		case Rule.ID:
			Element result = document.getElementById(resultTagName);
			results.add(result);
			break;
		case Rule.SELECTION:
			results = document.select(resultTagName);
			break;
		default:
			//当resultTagName为空时，默认body标签
			if (resultTagName.isEmpty()) {
				results = document.getElementsByTag("body");
			}
			break;
		}
		
		for (Element result : results) {
			
			Elements links = result.getElementsByTag("a");
			for (Element link : links) {
				//必要的筛选
				String linkHref = link.attr("href");
				String linkText = link.text();
				data = new LinkTypeData();
				data.setLinkText(linkText);
				data.setLinkHref(linkHref);
				datas.add(data);
			}
		}
		
		return datas;
		
	}

	private static void validateRule(Rule rule) {
		String url = rule.getUrl();
		if ("".equals(url) || url == null) {
			throw new RuleException("url 不能为空！！！");
		} 
		if (!url.startsWith("http://") && !url.startsWith("https://")) {
			throw new RuleException("url 格式不正确！！！");
		}
		if (rule.getParams() != null && rule.getValues() != null) {
			if (rule.getParams().length != rule.getValues().length) {
				throw new RuleException("参数的键值对个数不匹配！！！");
			}
		}
		
	}
}
